package com.huawei.pet.controller;

import com.huawei.basic.exception.BusinessException;
import com.huawei.basic.utils.LoginContext;
import com.huawei.org.domain.Employee;
import com.huawei.org.service.IEmployeeService;
import com.huawei.pet.service.IPetService;
import com.huawei.pet.domain.Pet;
import com.huawei.pet.query.PetQuery;
import com.huawei.basic.utils.AjaxResult;
import com.huawei.basic.utils.PageList;
import com.huawei.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    @Autowired
    private IEmployeeService employeeService;


    /**
     * 宠物上架接口
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxResult.success();
        }catch (BusinessException e) {//我们自己的业务错误  自定义异常
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 宠物下架接口
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.success();
        }catch (BusinessException e) {//我们自己的业务错误  自定义异常
            e.printStackTrace();
            return AjaxResult.error().setMessage(e.getMessage());
        }catch (Exception e) {//系统级别的错误
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getCurrentLoginInfo(request);
            Employee employee = employeeService.loadByLogininfoId(loginInfo.getId());
            pet.setShopId(employee.getShop_id());
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.save(pet);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping()
    public List<Pet> list(){

        return petService.loadAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return QueryResult 分页对象
     */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.queryPage(query);
    }
}
